package com.sskj.gocoin.v.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.adapter.slimadapter.IViewInjector;
import com.sskj.gocoin.adapter.slimadapter.SlimAdapter;
import com.sskj.gocoin.adapter.slimadapter.SlimInjector;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.component.DaggerUserDataComponent;
import com.sskj.gocoin.m.bean.FabiDetailListBean;
import com.sskj.gocoin.mvchelper.ModelRx2DataSource;
import com.sskj.gocoin.p.activity.FabiBuyDetailPresenter;
import com.sskj.gocoin.v.activity.FabiDetailActivity;
import com.sskj.gocoin.v.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 购买出售明细列表
 */
public class FabiBuyAndSellDetailFragment extends BaseFragment<FabiDetailActivity, FabiBuyDetailPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter<FabiDetailListBean> slimAdapter;
    private boolean isBuy;
    private MVCCoolHelper<List<FabiDetailListBean>> mvcCoolHelper;
    private MaterialDialog tipCancel;

    @Inject
    UserViewModel userViewModel;
    UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.recyclerview;
    }

    @Override
    public FabiBuyDetailPresenter getPresenter() {

        return new FabiBuyDetailPresenter();
    }

    public static FabiBuyAndSellDetailFragment newInstance(boolean isBuy) {

        Bundle args = new Bundle();
        args.putBoolean(Constants.IS_BUY, isBuy);
        FabiBuyAndSellDetailFragment fragment = new FabiBuyAndSellDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
        });
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.fabimineJavaFabiBuyAndSellDetailFragment2));
        statusMap.put("2", App.INSTANCE.getString(R.string.fabimineJavaFabiBuyAndSellDetailFragment4));
        statusMap.put("3", App.INSTANCE.getString(R.string.fabimineJavaFabiBuyAndSellDetailFragment6));
        isBuy = getArguments().getBoolean(Constants.IS_BUY);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setDividerColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 10)));
        //R.layout.recy_item_fabi_record,
        slimAdapter = SlimAdapter.create().register(R.layout.recy_item_mine_record, new SlimInjector<FabiDetailListBean>() {
            @Override
            public void onInject(FabiDetailListBean data, IViewInjector injector) {
                injector
                        .text(R.id.tv1, isBuy ? App.INSTANCE.getString(R.string.buyPrice) : App.INSTANCE.getString(R.string.fabimineXmlrecy_item_mine_record58))
                        .text(R.id.tv2, isBuy ? App.INSTANCE.getString(R.string.buyAmount) : App.INSTANCE.getString(R.string.fabimineXmlrecy_item_mine_record67))
                        .text(R.id.tv3, isBuy ? App.INSTANCE.getString(R.string.buyTotal) : App.INSTANCE.getString(R.string.fabimineXmlrecy_item_mine_record77))
//                        .text(R.id.tv_order_number, data.getOrder_num())
                        .text(R.id.tv_order_date, data.getAdd_time())
                        .text(R.id.tv_order_price, "￥" + data.getPrice())
                        .text(R.id.tv_order_amount, data.getTrans_num() + " USDT")
                        .text(R.id.tv_deal_money, "￥" + data.getTotalprice())
                        .text(R.id.tv_limit_small, "￥" + data.getMin_price())
                        .text(R.id.tv_limit_big, "￥" + data.getMax_price())
                        .visibility(R.id.iv_wechat, "1".equals(data.getPay_wx()) ? View.VISIBLE : View.GONE)
                        .visibility(R.id.iv_bank, "1".equals(data.getPay_backcard()) ? View.VISIBLE : View.GONE)
                        .visibility(R.id.iv_alipay, "1".equals(data.getPay_alipay()) ? View.VISIBLE : View.GONE)
                        .clicked(R.id.tv_order_status, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if ("1".equals(data.getCode())) {
                                    googleCheck(data.getId());
                                }
                            }
                        })
//                        .text(R.id.tv_charge, isBuy ? "- -" : data.getSxfee())
//                        .textColor(R.id.tv_order_status, ("1".equals(data.getCode()) || "2".equals(data.getCode())) ? getResources().getColor(R.color.market_up) : "3".equals(data.getCode()) ? getResources().getColor(R.color.market_down) : getResources().getColor(R.color.fabi_bottom_sheet_gray_color))
                        .text(R.id.tv_order_status, statusMap.get(data.getCode()));  // 明细状态--已付款 未付款
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<FabiDetailListBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiDetailListBean>)
                        page -> mPresenter.getList(page, 15, isBuy), 15);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
//        ItemClickSupport.addTo(recyclerView)
//                .setOnItemClickListener((recyclerView, position, v) -> {
//                    FabiSellDetail2Activity.start(getActivity(), slimAdapter.getDataItem(position).getOrder_num(), isBuy);
//                });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mvcCoolHelper != null) {
            mvcCoolHelper.refresh();
        }
    }

    public void cancelSuccess() {
        if (mvcCoolHelper != null) {
            mvcCoolHelper.refresh();
        }
    }

    /**
     * 谷歌验证
     */
    private void googleCheck(String id) {
      //  if (userData.getConfig().getGoogle_verify() == 1) {
        if (false) {
            TipUtil.googleCheckTip(getActivity(), (pwd) -> {
                mPresenter.requestGoogleCheck(pwd, id);
            });
        } else {
            //关闭之后不作处理
            mPresenter.cancelPublishOrder(isBuy, id);
        }
    }

    /**
     * 谷歌验证通过
     */
    public void googleCheckSuccess(String id) {
        mPresenter.cancelPublishOrder(isBuy, id);
    }
}
